// pages/feedback/feedback.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    feedbackTypes: [
      { name: '功能建议', value: 'suggestion', icon: '💡' },
      { name: '内容错误', value: 'error', icon: '❗' },
      { name: '合作沟通', value: 'problem', icon: '❓' },
      { name: '其他反馈', value: 'other', icon: '📝' }
    ],
    selectedType: 'suggestion',
    feedbackContent: '',
    contactInfo: '',
    images: [],
    contentLength: 0,
    showSuccess: false
  },

  /**
   * 选择反馈类型
   */
  selectType(e) {
    const type = e.currentTarget.dataset.value;
    this.setData({
      selectedType: type
    });
  },

  /**
   * 输入反馈内容
   */
  onContentInput(e) {
    const value = e.detail.value;
    this.setData({
      feedbackContent: value,
      contentLength: value.length
    });
  },

  /**
   * 输入联系方式
   */
  onContactInput(e) {
    this.setData({
      contactInfo: e.detail.value
    });
  },

  /**
   * 选择图片
   */
  chooseImage() {
    wx.chooseImage({
      count: 3 - this.data.images.length,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePaths = res.tempFilePaths;
        this.setData({
          images: [...this.data.images, ...tempFilePaths]
        });
      }
    });
  },

  /**
   * 删除图片
   */
  removeImage(e) {
    const index = e.currentTarget.dataset.index;
    const images = this.data.images.filter((_, i) => i !== index);
    this.setData({
      images
    });
  },

  /**
   * 提交反馈
   */
  submitFeedback(e) {
    const { content, contact } = e.detail.value;
    
    if (!content.trim()) {
      wx.showToast({
        title: '请填写反馈内容',
        icon: 'none'
      });
      return;
    }
    
    // 显示加载中
    wx.showLoading({
      title: '提交中...',
      mask: true
    });
    
    // 模拟提交到服务器
    setTimeout(() => {
      wx.hideLoading();
      
      // 显示成功提示
      this.setData({
        showSuccess: true
      });
      
      // 重置表单（保留反馈类型）
      this.setData({
        feedbackContent: '',
        contactInfo: '',
        images: [],
        contentLength: 0
      });
      
      // 记录反馈数据（实际开发中应发送到服务器）
      console.log('反馈提交成功', {
        type: this.data.selectedType,
        content,
        contact,
        images: this.data.images
      });
    }, 1500);
  },

  /**
   * 关闭成功提示
   */
  closeModal() {
    this.setData({
      showSuccess: false
    });
    wx.navigateBack();
  }
});